<?php

namespace app\modules\admin\controllers;
use Yii;
use yii\web\Controller;
use app\models\Test;
/**
 * Default controller for the `admin` module
 */
class DefaultController extends Controller
{
    /**
     * Renders the index view for the module
     * @return string
     */
    public function actionIndex()
    {
        //查询数据
        $rows = (new \yii\db\Query())
            ->select(['id', 'name','age','content'])
            ->from('test')
            ->orderBy('id desc')
            ->all();
        foreach($rows as $k=>$v){
            $rows[$k]['content']=json_decode($rows[$k]['content'],true);
            if(is_array($rows[$k]['content'])){
                foreach($rows[$k]['content'] as $kk=>$vv){
                    if($rows[$k]['content'][$kk]['name']=="lzn"){
                        $rows[$k]['admin']=$rows[$k]['content'][$kk]['name'];
                    }

                }
            }


        }
        //添加数据
        $data['name']="王大锤5";
        $data['age']=30;
        $data['content']= $array = array(
            array('name'=>'lzn','code_id'=>201,203),
            array('name'=>'km','code_id'=>208,215)

        );
        $data['content']=json_encode($data['content']);
        $user = new Test();
//        $user->name = '王大锤1';
//        $user->age = 28;
//        $res = $user->insert();
        $res=$user->Test_Add($data);
//        //tian
        $response = Yii::$app->response;
        $response->format = \yii\web\Response::FORMAT_JSON;
        if($res){
            $msg="添加成功";
            $code=200;
        }else{
            $msg="添加失败";
            $code=201;
        }
        return ['code'=>$code, 'msg'=>$msg];
        return ['code'=>200, 'msg'=>'成功','data'=>$rows];
//        return $this->render('index');
    }

    /**
     *该方法为测试方法
     */
    public function actionTest(){
        echo "hello word";
    }
    /**
     *该方法为测试方法
     */
    public function actionTest2(){
        echo "hello word";
    }
}
